﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using System.Data;

namespace DonutLandLibrary.Robots.Environments
{
    public class SquareRowCollection : IEnumerable<Square>, IEnumerator<Square>
    {
        private List<Square> SquareRowList;
        private int CurrentIndex;

        public int ID { get; set; }

        public int PowerSources
        {
            get
            {
                int count = 0;

                foreach (Square square in SquareRowList)
                {
                    count += square.PowerSources;
                }

                return count;
            }
        }

        public int CorrosivePatches
        {
            get
            {
                int count = 0;

                foreach (Square square in SquareRowList)
                {
                    if (square.CorrosivePatchPresent)
                    {
                        count++;
                    }
                }

                return count;
            }
        }

        public SquareRowCollection(int rowSize, int y)
        {
            Square squareItem;
            SquareRowList = new List<Square>();

            //create a square for each count of the row size
            for (int i = 0; i < rowSize; i++)
            {
                //instantiate the new square
                squareItem = new Square(i, y);

                //add the square to the collection
                SquareRowList.Add(squareItem);
            }

            Reset();
        }

        public Square retrieveSquareAtCoordinate(int x)
        {
            return SquareRowList[x];
        }

        public object renderSquareRow(Square.renderFormat renderFormat, Object inputData = null)
        {
            object output = null;

            switch (renderFormat)
            {
                case Square.renderFormat.html:

                    output = renderSquareRowHTML();

                    break;
                case Square.renderFormat.xml:

                    throw new NotSupportedException("XML output is not currently supported");

                    //break;
                case Square.renderFormat.json:

                    throw new NotSupportedException("JSON output is not currently supported");

                    //break;

                case Square.renderFormat.data:

                    output = renderSquareRowData((DataTable)inputData);

                    break;
            }

            return output;
        }

        private string renderSquareRowHTML()
        {
            StringBuilder builder = new StringBuilder();

            builder.Append("<tr>");

            foreach(Square square in SquareRowList){

                builder.Append(square.renderSquare(Square.renderFormat.html));

            }

            builder.Append("</tr>");

            return builder.ToString();
        }

        private DataRow renderSquareRowData(DataTable inputData)
        {
            DataRow datarow = inputData.NewRow();

            //create a square for each count of the row size
            for (int i = 0; i < SquareRowList.Count; i++)
            {
                datarow[i] = SquareRowList[i].renderSquare(Square.renderFormat.data);
            }

            return datarow;
        }

        public IEnumerator<Square> GetEnumerator()
        {
            return this;
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        public Square Current
        {
            get 
            { 
                return SquareRowList[CurrentIndex];
            }
        }

        public void Dispose()
        {
            //TODO : what to dispose?
        }

        object System.Collections.IEnumerator.Current
        {
            get
            {
                return Current;
            }
        }

        public bool MoveNext()
        {
            CurrentIndex++;

            if (CurrentIndex < SquareRowList.Count)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public void Reset()
        {
            CurrentIndex = -1;
        }
    }
}

